<template>
  <el-dialog :visible="showRoleDialog" title="分配角色" @close="cancelDialog">
    <el-checkbox-group v-model="ids" style="margin-bottom: 20px">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row type="flex" justify="center">
      <el-button type="primary" @click="save">确定</el-button>
      <el-button @click="cancelDialog">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getAllRoles } from "@/api/role";
import { userProfile } from "@/api/user";
import { setRoles } from "@/api/employees";
export default {
  name: "roleSetting",
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    userId: {
      type: String,
      default: "",
      required: true,
    },
  },
  data() {
    return {
      ids: [],
      roleList: [],
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    cancelDialog() {
      this.$emit("update:showRoleDialog", false);
    },
    async getRoleList() {
      let res = await getAllRoles({ page: 1, pagesize: 99999 });
      this.roleList = res.data.rows;
    },
    async getUserProfile(id) {
      let res = await userProfile(id);
      this.ids = res.data.roleIds;
    },
    async save() {
      await setRoles({ id: this.userId, roleIds: this.ids });
      this.$message.success("分配角色成功");
      this.cancelDialog();
    },
  },
};
</script>

<style lang="scss">
</style>